package D12_9z.xml练习题.xml解析_4;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo1 {
    static ArrayList<Phone> arr = new ArrayList<>();
    public static void main(String[] args) throws Exception{
        Scanner sca = new Scanner(System.in);
        while (true){
            System.out.println("【查询手机】\n" +
                    "1.黑色   2.白色   3.银色   4.金色    5.退出");
            int num = sca.nextInt();
            if (num==1){
                run1("黑色");
                run2();
            }
            if (num==2){
                run1("白色");
                run2();
            }
            if (num==3){
                run1("银色");
                run2();
            }
            if (num==4){
                run1("金色");
                run2();
            }
            if (num==5){
                return;
            }
        }
    }
    public static void run1(String color)throws Exception{
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read("src/Y2019/M12_/D12_9/xml练习题/xml解析_4/phones.xml");
        Element root = read.getRootElement();
        List<Element> product = root.elements("product");
        for (Element e : product) {
            Phone phone = new Phone();
            if (e.elementText("color").equals(color)){
                phone.setName(e.elementText("name"));
                phone.setBrand(e.elementText("brand"));
                phone.setSize(Double.parseDouble(e.elementText("size")));
                phone.setPrice(Integer.parseInt(e.elementText("price")));
                phone.setColor(e.elementText("color"));
                arr.add(phone);
                return;
            }
        }
        System.out.println("没有此颜色的手机!!!");
    }
    public static void run2(){
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i));
        }
    }
}
